﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Device.Location;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Windows.Devices.Geolocation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Shell;
using SMARTMobile.Common;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.ViewModels;
using SMARTMobile.Models.UI;
using Microsoft.Phone.Maps.Toolkit;
using SmartMobile.Data;
using Telerik.Windows.Controls;
using SMARTMobile.Models;
using C1.Phone.FlexGrid;

namespace SMARTMobile.Views
{
    public partial class PhanTuyenPage
    {
        private bool _isSelectedDay;
        private bool _isSelectedWeek;
        SmartMobileDBContext _smartMobileContext;
        
        public PhanTuyenPage()
        {
            InitializeComponent();
            this._smartMobileContext = new SmartMobileDBContext(SmartMobileDBContext.ConnectionString);
            this._smartMobileContext.CreateIfNotExists();
            this._smartMobileContext.LogDebug = true;

            var arClass = this.FlexGrid.Columns["OutletTypeCode"];
            arClass.ValueConverter = new ColumnValueConverter((this.DataContext as PhanTuyenViewModel).OutletTypeList);

            var arSubClass = this.FlexGrid.Columns["GlobalTypeCode"];
            arSubClass.ValueConverter = new ColumnValueConverter((this.DataContext as PhanTuyenViewModel).OutletGlobalTypeList);

            var freq = this.FlexGrid.Columns["Freq"];
            freq.ValueConverter = new ColumnValueConverter((this.DataContext as PhanTuyenViewModel).FreqList);

            //var strategicType = this.FlexGrid.Columns["GFKAreaCode"];
            //strategicType.ValueConverter = new ColumnValueConverter((this.DataContext as PhanTuyenViewModel).GFKAreaList);

            this.KhachHangAutoComplete.SuggestionsSource = this._smartMobileContext.ARCUST.ToList();
            this.KhachHangAutoComplete.SuggestionSelected += KhachHangAutoComplete_SuggestionSelected;
            this.KhachHangAutoComplete.FilterKeyProvider = (object item) =>
            {
                ARCUST typedItem = item as ARCUST;
                return string.Format("{0} - {1}", typedItem.Customername, typedItem.Address);
            };
        }

        private void KhachHangAutoComplete_SuggestionSelected(object sender, Telerik.Windows.Controls.SuggestionSelectedEventArgs e)
        {
            ARCUST selectedCus = e.SelectedSuggestion as ARCUST;
            if (selectedCus != null)
            {
                (DataContext as PhanTuyenViewModel).TCARCustNewObservable = new ObservableCollection<TCARCustModel>
                    ((DataContext as PhanTuyenViewModel).AllTCARCustNew.Where(t => t.Customerkey == selectedCus.Customerkey));                
            }
        }

        private void NhanVienListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.GetCustomerList();            
        }

        private void NgayListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this._isSelectedDay = true;
            this.GetCustomerList();
        }

        private void GetCustomerList()
        {
            ARSALESP selecedSaleSP = this.DoiTuongLP.SelectedItem as ARSALESP;
            List<TCARCUST> tcARCustList = null;
            if (selecedSaleSP.Salesp_key != null && this._isSelectedDay)
            {
                string selectedNgay = this.NgayLP.SelectedItem as string;

                if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T2)
                {
                    tcARCustList = _smartMobileContext.TCARCUST.Where(x => x.Salespkey == selecedSaleSP.Salesp_key && x.T2 == "1").ToList();
                    this.GetTCARCustByWhere(tcARCustList);                 
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T3)
                {
                    tcARCustList = _smartMobileContext.TCARCUST.Where(x => x.Salespkey == selecedSaleSP.Salesp_key && x.T3 == "1").ToList();
                    this.GetTCARCustByWhere(tcARCustList); 
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T4)
                {
                    tcARCustList = _smartMobileContext.TCARCUST.Where(x => x.Salespkey == selecedSaleSP.Salesp_key && x.T4 == "1").ToList();
                    this.GetTCARCustByWhere(tcARCustList); 
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T5)
                {
                    tcARCustList = _smartMobileContext.TCARCUST.Where(x => x.Salespkey == selecedSaleSP.Salesp_key && x.T5 == "1").ToList();
                    this.GetTCARCustByWhere(tcARCustList); 
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T6)
                {
                    tcARCustList = _smartMobileContext.TCARCUST.Where(x => x.Salespkey == selecedSaleSP.Salesp_key && x.T6 == "1").ToList();
                    this.GetTCARCustByWhere(tcARCustList); 
                }
                else if (selectedNgay == Localization.AppResources.ROUTECHECK_LABEL_T7)
                {
                    tcARCustList = _smartMobileContext.TCARCUST.Where(x => x.Salespkey == selecedSaleSP.Salesp_key && x.T7 == "1").ToList();
                    this.GetTCARCustByWhere(tcARCustList); 
                }
                else
                {
                    tcARCustList = _smartMobileContext.TCARCUST.Where(x => x.Salespkey == selecedSaleSP.Salesp_key).ToList();
                    this.GetTCARCustByWhere(tcARCustList); 
                }
            }
        }

        private void GetTCARCustModel(List<TCARCUST> tcARCustList)
        {
            (DataContext as PhanTuyenViewModel).TCARCustObservable = new ObservableCollection<TCARCustModel>();
            foreach (var item in tcARCustList.OrderBy(x => x.Customerkey))
            {                
                TCARCustModel model = new TCARCustModel
                {
                    Customerkey = item.Customerkey,
                    CustomerName = item.Customername,
                    IsSelected = false,
                    Address = item.Address,
                    T2 = item.T2 == "1" ? true : false,
                    T3 = item.T3 == "1" ? true : false,
                    T4 = item.T4 == "1" ? true : false,
                    T5 = item.T5 == "1" ? true : false,
                    T6 = item.T6 == "1" ? true : false,
                    T7 = item.T7 == "1" ? true : false,
                    Freq = (item.Freq).ToString() ?? string.Empty,
                    GlobalTypeCode = item.GlobalTypeCode,
                    OutletTypeCode = item.OutletTypeCode,
                    GFKAreaCode = item.GFKArea,   
                };

                (DataContext as PhanTuyenViewModel).TCARCustObservable.Add(model);                
            }
        }

        private void GetTCARCustByWhere(List<TCARCUST> list)
        {
            if (this._isSelectedWeek)
            {
                string selecteWeek = this.TuanLP.SelectedItem as string;

                if (selecteWeek == Localization.AppResources.ROUTECHECK_LABEL_1)
                {
                    this.GetTCARCustModel(list.Where(x => x.Week == Localization.AppResources.ROUTECHECK_LABEL_1).ToList());
                }
                else if (selecteWeek == Localization.AppResources.ROUTECHECK_LABEL_2)
                {
                    this.GetTCARCustModel(list.Where(x => x.Week == Localization.AppResources.ROUTECHECK_LABEL_2).ToList());
                }
                else if (selecteWeek == Localization.AppResources.ROUTECHECK_LABEL_3)
                {
                    this.GetTCARCustModel(list.Where(x => x.Week == Localization.AppResources.ROUTECHECK_LABEL_3).ToList());
                }
                else if (selecteWeek == Localization.AppResources.ROUTECHECK_LABEL_4)
                {
                    this.GetTCARCustModel(list.Where(x => x.Week == Localization.AppResources.ROUTECHECK_LABEL_4).ToList());
                }
                else 
                {
                    this.GetTCARCustModel(list);
                }
            }
        }

        private void ExitModalWindow_Click(object sender, RoutedEventArgs e)
        {
            this.ChuyenTuyenWindow.IsOpen = false;
        }

        private void ChuyenTuyenButton_Click(object sender, RoutedEventArgs e)
        {
            this.ChuyenTuyenWindow.IsOpen = true;

            List<ARSALESP> arSale = _smartMobileContext.ARSALESP.ToList();
            arSale.Remove((DataContext as PhanTuyenViewModel).SelectedARSaleSP);

            //this.listButton.ItemsSource = arSale;

            List<TCARCustModel> tcArCust = (DataContext as PhanTuyenViewModel).TCARCustObservable.Where(t => t.IsSelected == true).ToList();            
        }

        private void TuanLP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this._isSelectedWeek = true;
            this.GetCustomerList();
        }
    }
} 